package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.secure.utils.AuthUtil;
import com.guodi.model.entity.IdtFillTaskMessage;
import com.guodi.model.enums.IdtFillTaskMessageResolvedEnum;
import com.guodi.model.enums.IdtFillTaskMessageTypeEnum;
import com.guodi.model.enums.IdtFillTaskStatusEnum;
import com.guodi.model.mapper.IdtFillTaskMessageMapper;
import com.guodi.model.service.IIdtFillTaskMessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 指标填报任务消息表 服务实现类
* @author lzy
* @date 2023-05-10
*/
@Service
public class IdtFillTaskMessageServiceImpl extends ServiceImpl<IdtFillTaskMessageMapper, IdtFillTaskMessage> implements IIdtFillTaskMessageService {

    @Resource
    private IdtFillTaskMessageMapper idtFillTaskMessageMapper;


    @Override
    public IdtFillTaskMessage saveMessage(String taskId, String code, String message) {
        LambdaQueryWrapper<IdtFillTaskMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(IdtFillTaskMessage::getTaskId,taskId);
        wrapper.eq(IdtFillTaskMessage::getResolved,IdtFillTaskMessageResolvedEnum.UNTREATED.getCode());
        wrapper.eq(IdtFillTaskMessage::getType,code);
        List<IdtFillTaskMessage> oldData = this.list(wrapper);
        if(CollectionUtils.isNotEmpty(oldData)){
            return oldData.get(0);
        }

        IdtFillTaskMessage entity = new IdtFillTaskMessage();
        prepareData(entity,true);
        entity.setTaskId(taskId);
        entity.setMessage(message);
        entity.setType(code);
        entity.setResolved(IdtFillTaskMessageResolvedEnum.UNTREATED.getCode());
        this.save(entity);
        return entity;
    }

    @Override
    public IPage<IdtFillTaskMessage> fillTaskMessages(Page page, String resolved) {
        String regionCode = AuthUtil.getUserRegionCode();
        this.autoPushMessages(regionCode);
        IPage<IdtFillTaskMessage> iPage = idtFillTaskMessageMapper.fillTaskMessages(page,regionCode,resolved);
        List<IdtFillTaskMessage> list = iPage.getRecords();
        if(CollectionUtils.isNotEmpty(list)){
            for(IdtFillTaskMessage message : list){
                message.setType(IdtFillTaskMessageTypeEnum.getNameByStatus(message.getType()));
                message.setResolved(IdtFillTaskMessageResolvedEnum.getNameByStatus(message.getResolved()));
                message.setTaskName(message.getTaskName()  + IdtFillTaskStatusEnum.getStatusByCode(message.getTaskStatus()));
            }
        }
        return iPage;
    }

    @Override
    public Integer messageCount() {
        String regionCode = AuthUtil.getUserRegionCode();
        this.autoPushMessages(regionCode);
        Integer count = idtFillTaskMessageMapper.messageCount(regionCode,IdtFillTaskMessageResolvedEnum.UNTREATED.getCode());
        return count;
    }

    @Override
    public void processedTask(String taskId) {
        LambdaUpdateWrapper<IdtFillTaskMessage> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(IdtFillTaskMessage::getResolved,IdtFillTaskMessageResolvedEnum.PROCESSED.getCode());
        wrapper.set(IdtFillTaskMessage::getModifier,AuthUtil.getUserId().toString());
        wrapper.set(IdtFillTaskMessage::getModifyTime,new Date());
        wrapper.eq(IdtFillTaskMessage::getTaskId,taskId);
        wrapper.eq(IdtFillTaskMessage::getResolved,IdtFillTaskMessageResolvedEnum.UNTREATED.getCode());
        this.update(wrapper);
    }

    private void prepareData(IdtFillTaskMessage entity, boolean isSave) {
        if(isSave){
            entity.setId(UuidUtil.get32UUID());
            entity.setCreator(String.valueOf(AuthUtil.getUserId()));
            entity.setCreateTime(new Date());
            //防止误修改
            entity.setModifier(null);
            entity.setModifyTime(null);
        }else{
            entity.setModifier(String.valueOf(AuthUtil.getUserId()));
            entity.setModifyTime(new Date());
            //防止误修改
            entity.setCreator(null);
            entity.setCreateTime(null);
        }
    }

    /**
     * @描述: 自动推送催办消息
     * @入参:
     * @出参:
     * @作者: lzy
     * @日期: 2023/5/23 15:38
     **/
    private synchronized void autoPushMessages(String regionCode){
        //先查出需要催办的任务id,离截止时间7天需要催办
        List<String> needTaskIds = this.idtFillTaskMessageMapper.toUrgingTask(regionCode);
        //查询已催办且未处理消息的任务id
        List<String>  taskIds = this.idtFillTaskMessageMapper.alreadyUrgingTask(regionCode,IdtFillTaskMessageTypeEnum.URGING.getCode(),IdtFillTaskMessageResolvedEnum.UNTREATED.getCode());

        if(CollectionUtils.isNotEmpty(needTaskIds)){
            List<IdtFillTaskMessage> messages = new ArrayList<>();
            for(String taskId : needTaskIds){
                if(!taskIds.contains(taskId)){
                    IdtFillTaskMessage message = new IdtFillTaskMessage();
                    message.setType(IdtFillTaskMessageTypeEnum.URGING.getCode());
                    message.setResolved(IdtFillTaskMessageResolvedEnum.UNTREATED.getCode());
                    message.setTaskId(taskId);
                    prepareData(message,true);
                    messages.add(message);
                }
            }
            if(CollectionUtils.isNotEmpty(messages)){
                this.saveBatch(messages);
            }
        }
    }
}
